package site.luojie.utils;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

/**
 * @Description:
 * @Author jie.luo
 * @Create: 2020-05-06 01:13
 **/
public class ClassFieldUtil {

    /**
     * 判断类是否有该注解
     *
     * @param clazz           类
     * @param annotationClass 注解class
     * @return true-包含
     */
    public static boolean classConstantsAnnotation(Class<?> clazz, Class<?> annotationClass) {
        Annotation[] fieldAnnotations = clazz.getAnnotations();
        for (Annotation fieldAnnotation : fieldAnnotations) {
            if (fieldAnnotation.annotationType() == annotationClass) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断字段是否有该注解
     *
     * @param field           字段
     * @param annotationClass 注解class
     * @return true-包含
     */
    public static boolean fieldConstantsAnnotation(Field field, Class<?> annotationClass) {
        Annotation[] fieldAnnotations = field.getAnnotations();
        for (Annotation fieldAnnotation : fieldAnnotations) {
            if (fieldAnnotation.annotationType() == annotationClass) {
                return true;
            }
        }
        return false;
    }

}
